// pages/my-pages/top-up-add/index.js
const httpUtils = require('../../../utils/httpUtils')
const ui = require('../../../utils/ui')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    member: {},
    showMemberValue: '请选择会员',
    addForm: {
      memberId: null,
      topUpPrice: null,
      giftPrice: null
    },
    voucherList: []
  },
  //点击了确定按钮
  confirmSaveButton: function (e) {
    console.log('点击了保存')
    let voucherList = this.data.voucherList;
    this.setData({
      'addForm.voucherList': voucherList
    })
    let obj = {
      method: 'POST',
      showLoading: true,
      url: 'topUpBill/addTopUp',
      data: this.data.addForm
    }
    httpUtils.request(obj).then(res => {
      if (res.data.retCode == 0) {
        ui.showToast('保存成功')
        wx.navigateBack({
          detail: 1
        })
      } else {
        ui.showToast(res.data.message)
      }
    }).catch(err => {
      ui.showToast('请求失败')
    });
  },
  //点击了会员列表
  checkMemberButton: function (e) {
    console.log('点击了会员列表')
    wx.navigateTo({
      url: '/pages/my-pages/select-member/index',
    })
  },
  //点击了代金券列表
  checkVoucherButton: function (e) {
    console.log('点击了代金券列表')
    wx.navigateTo({
      url: '/pages/my-pages/select-voucher/index',
    })
  },
  //金额判断
  onPriceInput: function (e) {
    let defaultPrice = e.detail.value;
    defaultPrice = defaultPrice.replace(/\D/g, '')
    this.setData({
      'addForm.topUpPrice': defaultPrice
    })
  },
  //金额判断
  onGiftInput: function (e) {
    let defaultPrice = e.detail.value;
    defaultPrice = defaultPrice.replace(/\D/g, '')
    this.setData({
      'addForm.giftPrice': defaultPrice
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let that = this;
    let member = that.data.member
    if (member != null) {
      if (member.id != null && member.id != undefined) {
        that.setData({
          'addForm.memberId': member.id
        })
      }
      if (member.memberName != null && member.memberName != undefined && member.memberNo != null && member.memberNo != undefined) {
        that.setData({
          showMemberValue: member.memberName + ' (NO：' + member.memberNo + ')'
        })
      }
    }
    if (null != that.data.showMemberValue && that.data.showMemberValue.length > 11) {
      that.setData({
        showMemberValue: that.data.showMemberValue.slice(0, 11)
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})